package com.bocai.yuweiwangluo.activity;


import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.PopBean;
import com.bocai.yuweiwangluo.presenter.ReleaseMatchPresenter;
import com.bocai.yuweiwangluo.utils.DateTimePickDialogUtil;
import com.bocai.yuweiwangluo.utils.MyConst;
import com.bocai.yuweiwangluo.utils.PopUtil;
import com.bocai.yuweiwangluo.viewModel.IReleaseMatchView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


/**
 * 发布约战界面
 */
public class ReleaseMatchActivity extends BaseActivity implements IReleaseMatchView, View.OnClickListener
{
    @ViewInject(R.id.tv_releasematch_starttime)
    TextView tv_releasematch_starttime;
    @ViewInject(R.id.tv_releasematch_endtime)
    TextView tv_releasematch_endtime;
    @ViewInject(R.id.ll_releasematch_starttime)
    LinearLayout ll_releasematch_starttime;
    @ViewInject(R.id.ll_releasematch_endtime)
    LinearLayout ll_releasematch_endtime;

    @ViewInject(R.id.ll_releasematch_personcount)
    LinearLayout llPersonCount;
    @ViewInject(R.id.tv_releasematch_personcount)
    TextView tvPersonCount;
    @ViewInject(R.id.tv_releasematch_football)
    TextView tvFootball;
    @ViewInject(R.id.tv_releasematch_basketball)
    TextView tvBasketball;
    @ViewInject(R.id.tv_releasematch_yumao)
    TextView tvYumao;
    @ViewInject(R.id.tv_releasematch_wangqiu)
    TextView tvWangqiu;
    @ViewInject(R.id.tv_releasematch_aa)
    TextView tvAA;
    @ViewInject(R.id.tv_releasematch_dinner)
    TextView tvDinner;
    @ViewInject(R.id.et_releasematch_address)
    EditText etAddr;
    @ViewInject(R.id.btn_releasematch)
    Button btnReleasrmatch;
    @ViewInject(R.id.ll_releasematch_football)
    LinearLayout llFootball;
    @ViewInject(R.id.ll_releasematch_basketball)
    LinearLayout llBasketball;
    @ViewInject(R.id.ll_releasematch_yumao)
    LinearLayout llYumaoqiu;
    @ViewInject(R.id.ll_releasematch_wangqiu)
    LinearLayout llWangqiu;
    @ViewInject(R.id.et_releasematch_indicating)
    EditText etContent;
    @ViewInject(R.id.ll_releasematch_area)
    LinearLayout llArea;
    @ViewInject(R.id.tv_releasematch_area)
    TextView tvArea;
    private DateTimePickDialogUtil dateTimePicKDialog;
    private PopUtil pu;
    private int width;
    private ArrayList<PopBean> list;
    private ArrayList<PopBean> arealist;
    private String price="";
    private String sportType_id="";
    private String payType="";
    private PopupWindow addFriendPopupWindow;
    private TextView item_small_reporter_right_nearby;
    private EditText item_add_friends;
    private ReleaseMatchPresenter presenter;
    private String num="0";
    private String cityType="";
    @Override
    protected int contentView()
    {
        return R.layout.activity_releasematch;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("发布约战");
        getReleasematchPresenter();
        WindowManager windowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
        width=windowManager.getDefaultDisplay().getWidth();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time=sdf.format(new Date(System.currentTimeMillis()));
        dateTimePicKDialog = new DateTimePickDialogUtil(this, time);
        initEvent();
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        ll_releasematch_starttime.setOnClickListener(this);
        ll_releasematch_endtime.setOnClickListener(this);
        llPersonCount.setOnClickListener(this);
        tvAA.setOnClickListener(this);
        tvDinner.setOnClickListener(this);
        llFootball.setOnClickListener(this);
        llBasketball.setOnClickListener(this);
        llYumaoqiu.setOnClickListener(this);
        llWangqiu.setOnClickListener(this);
        btnReleasrmatch.setOnClickListener(this);
        llArea.setOnClickListener(this);
    }
    private void showPopwindow(View v,final TextView tv,ArrayList<PopBean> list, final int what)
    {
        pu= PopUtil.getInstance(this, v, width, width/2 , list);
        pu.setListener(new PopUtil.OnDatePopClickListener() {
            @Override
            public void onClick(PopUtil pu, PopBean date) {
                tv.setText(date.getName());
                pu.dismiss();
                if(what==0){
                    num = date.getName();
                }else if(what==1){
                    cityType=date.getId();
                }
            }
        });
        pu.show();
    }
    private ArrayList<PopBean> setTypeList()
    {
        list=new ArrayList<>();
        for (int i=0;i<100;i++)
        {
            PopBean popBean=new PopBean();
            popBean.setId(i+1+"");
            popBean.setName(i+1+"");
            list.add(i,popBean);
        }
        return list;
    }
    private ArrayList<PopBean> setCityList()
    {
        try{
            arealist=new ArrayList<>();
            if(MyConst.citys.size()!=0)
            {

                for (int i=0;i<MyConst.citys.size();i++)
                {
                    PopBean popBean=new PopBean();
                    popBean.setId(MyConst.citys.get(i).getId());
                    popBean.setName(MyConst.citys.get(i).getName());
                    arealist.add(i, popBean);
                }
            }
        }catch (Exception e){}
        return arealist;

    }
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_releasematch_area:
                showPopwindow(v,tvArea,setCityList(),1);
                break;
            case R.id.ll_releasematch_endtime:
                dateTimePicKDialog.dateTimePicKDialog(tv_releasematch_endtime);
                break;
            case R.id.ll_releasematch_starttime:
                dateTimePicKDialog.dateTimePicKDialog(tv_releasematch_starttime);
                break;
            case R.id.ll_releasematch_personcount:
                //选择人数
                showPopwindow(v,tvPersonCount,setTypeList(),0);
                break;
            case R.id.tv_releasematch_aa:
                final View pupopView = this.getLayoutInflater().inflate(R.layout.item_add_friends, null, false);
                DisplayMetrics dm = new DisplayMetrics();
                this.getWindowManager().getDefaultDisplay().getMetrics(dm);
                addFriendPopupWindow = new PopupWindow(pupopView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
                addFriendPopupWindow.setOutsideTouchable(true);
                // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
                addFriendPopupWindow.setBackgroundDrawable(new BitmapDrawable());
                //pupopView的title
                item_small_reporter_right_nearby=(TextView)pupopView.findViewById(R.id.item_small_reporter_right_nearby);
                item_small_reporter_right_nearby.setText("设置AA付款金额");
                item_add_friends=(EditText)pupopView.findViewById(R.id.item_add_friends);
                item_add_friends.setHint("设置AA付款金额");
                //全屏
                pupopView.findViewById(R.id.item_add_friends_linear).setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        addFriendPopupWindow.dismiss();
                    }
                });

                //取消
                pupopView.findViewById(R.id.item_small_reporter_right_province).setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        addFriendPopupWindow.dismiss();
                    }
                });
                //确定
                pupopView.findViewById(R.id.item_small_reporter_right_country).setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        tvAA.setText("AA制("+item_add_friends.getText()+"元)");
                        price=item_add_friends.getText().toString();
                        addFriendPopupWindow.dismiss();
                    }
                });
                addFriendPopupWindow.showAtLocation(this.findViewById(R.id.releasematch), Gravity.CENTER,0,0);
                //选择AA制
                tvDinner.setBackgroundColor(getResources().getColor(R.color.white));
                tvDinner.setTextColor(getResources().getColor(R.color.textcolor_696969));
                tvAA.setBackgroundColor(getResources().getColor(R.color.title_bag));
                tvAA.setTextColor(getResources().getColor(R.color.white));
                payType="1";
                break;
            case R.id.tv_releasematch_dinner:
                //选择我请客
                tvAA.setText("AA");
                tvAA.setBackgroundColor(getResources().getColor(R.color.white));
                tvAA.setTextColor(getResources().getColor(R.color.textcolor_696969));
                tvDinner.setTextColor(getResources().getColor(R.color.white));
                tvDinner.setBackgroundColor(getResources().getColor(R.color.title_bag));
                price=0+"";
                payType="2";
                break;
            case R.id.ll_releasematch_football:
                //选择足球
                setTextColor();
                tvFootball.setTextColor(getResources().getColor(R.color.title_bag));
                getSportType_id("足球");
                break;
            case R.id.ll_releasematch_basketball:
                //选择篮球
                setTextColor();
                tvBasketball.setTextColor(getResources().getColor(R.color.title_bag));
                getSportType_id("篮球");
                break;
            case R.id.ll_releasematch_yumao:
                //选择羽毛球
                setTextColor();
                tvYumao.setTextColor(getResources().getColor(R.color.title_bag));
                getSportType_id("羽毛球");
                break;
            case R.id.ll_releasematch_wangqiu:
                //选择网球
                setTextColor();
                tvWangqiu.setTextColor(getResources().getColor(R.color.title_bag));
                getSportType_id("网球");
                break;
            case R.id.btn_releasematch:
                presenter.releaseMatch();
                break;
        }
    }
    private String  getSportType_id(String SportType)
    {
        try
        {
            if(MyConst.type!=null)
            {
                for(int i=0;i<MyConst.type.size();i++)
                {
                    if(SportType.equals(MyConst.type.get(i).getTitle()))
                    {
                        sportType_id=MyConst.type.get(i).getId();
                    }
                }
            }
        }catch (Exception e)
        {
        }
        return "";
    }
    private void setTextColor()
    {
        tvFootball.setTextColor(getResources().getColor(R.color.black));
        tvBasketball.setTextColor(getResources().getColor(R.color.black));
        tvYumao.setTextColor(getResources().getColor(R.color.black));
        tvWangqiu.setTextColor(getResources().getColor(R.color.black));
    }

    @Override
    public String type_id()
    {
        return sportType_id;
    }

    @Override
    public String city_id()
    {
        return cityType;
    }

    @Override
    public String num()

    {
        return num;
    }

    @Override
    public String match_type()
    {
        return payType;
    }

    @Override
    public String content()
    {
        return etContent.getText().toString();
    }

    @Override
    public String stime()
    {
        return tv_releasematch_starttime.getText().toString() ;
    }

    @Override
    public String etime()
    {
        return tv_releasematch_endtime.getText().toString();
    }

    @Override
    public String addr()
    {
        return etAddr.getText().toString();
    }

    @Override
    public String price()
    {
        return price;
    }

    @Override
    public void finishActivity()
    {
        this.finish();
    }

    @Override
    public ReleaseMatchPresenter getReleasematchPresenter()
    {
        presenter=new ReleaseMatchPresenter(this,this,this);
        return presenter;
    }
}
